package com.example.shoppingdemo.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.ArrayRes;
import androidx.annotation.IdRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.example.shoppingdemo.Center.CenterClass;
import com.example.shoppingdemo.Center.LeftAdapter;
import com.example.shoppingdemo.Center.LeftModel;
import com.example.shoppingdemo.Center.ProtuctClass;
import com.example.shoppingdemo.Center.RightAdapter;
import com.example.shoppingdemo.Center.RightModel;
import com.example.shoppingdemo.R;
import com.example.shoppingdemo.nslcommodity.CommodityList;

import java.util.ArrayList;
import java.util.List;

/*
* 分类*/
public class ClassifyFragment extends Fragment {
    View messageLayout;
    private ProtuctClass protuctClass;
    List<LeftModel> leftData=new ArrayList<>();
    List<RightModel> rightModels=new ArrayList<>();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_classify, container, true);
    }
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        protuctClass=new CenterClass();
        initView();
    }
    private void initView(){
        RecyclerView categoryMenu=findViewById(R.id.center_item);
        RecyclerView deptMenu=findViewById(R.id.center_list);

        categoryMenu.setLayoutManager(new LinearLayoutManager(requireContext()));
        deptMenu.setLayoutManager(new StaggeredGridLayoutManager(
                4,StaggeredGridLayoutManager.VERTICAL));


        List<RightModel>rightData=new ArrayList<>();
        LeftAdapter leftAdapter=new LeftAdapter(requireActivity());
        RightAdapter rightAdapter=new RightAdapter(requireActivity());
        leftAdapter.setLeftData(leftData);

        rightAdapter.setRightData(rightData);
        categoryMenu.setAdapter(leftAdapter);
        deptMenu.setAdapter(rightAdapter);
        protuctClass.requestProductClass(new CenterClass.OnClassoficationResultListener() {
            @Override
            public void onClassoficationResult(List<LeftModel> modelList) {
                leftData.clear();
                leftData.addAll(modelList);
                leftData.get(0).setSelect(true);
                leftAdapter.setLeftData(leftData);
                leftAdapter.notifyDataSetChanged();
                rightData.clear();
                rightAdapter.setRightData(leftData.get(0).getRightModels());
                rightAdapter.notifyDataSetChanged();
            }
        });
        leftAdapter.setListener(new LeftAdapter.onLeftItemClickListener() {
            @Override
            public void onLeftItemClick(int position) {
                for (int i=0;i<leftData.size();i++){
                    leftData.get(i).setSelect(false);
                }
                leftData.get(position).setSelect(true);
                leftAdapter.notifyDataSetChanged();
                switch (position){
                    case 0:
                        rightData.clear();
                        rightModels=leftData.get(0).getRightModels();
                        rightAdapter.setRightData(leftData.get(0).getRightModels());
                        break;
                    case 1:
                        rightData.clear();
                        rightModels=leftData.get(1).getRightModels();
                        rightAdapter.setRightData(leftData.get(1).getRightModels());
                        break;
                    case 2:
                        rightData.clear();
                        rightModels=leftData.get(2).getRightModels();
                        rightAdapter.setRightData(leftData.get(2).getRightModels());
                        break;
                    case 3:
                        rightData.clear();
                        rightModels=leftData.get(3).getRightModels();
                        rightAdapter.setRightData(leftData.get(3).getRightModels());
                        break;
                    case 4:
                        rightData.clear();
                        rightModels=leftData.get(4).getRightModels();
                        rightAdapter.setRightData(leftData.get(4).getRightModels());
                        break;
                }
                rightAdapter.notifyDataSetChanged();
            }
        });
        leftAdapter.setLeftData(leftData);
        rightAdapter.setRightData(rightData);
        rightAdapter.setListener(new RightAdapter.onRightItemClickListener() {
            @Override
            public void onRightItemClick(int position) {
             Intent intent=new Intent(requireActivity(),CommodityList.class);
                intent.putExtra("id",rightModels.get(position).getId());
             startActivity(intent);
            }
        });
        categoryMenu.setAdapter(leftAdapter);
        deptMenu.setAdapter(rightAdapter);
    }
    private List<RightModel>getResToList(@ArrayRes int arrayRes) {
        String[] depts = getResources().getStringArray(arrayRes);
        List<RightModel> result = new ArrayList<>();
        for (int i = 0; i < depts.length; i++) {
            RightModel model = new RightModel();
            model.setName(depts[i]);

            result.add(model);
        }
        return result;
    }

    private <T extends View>T findViewById(@IdRes int resId)
    {
        return getView().findViewById(resId);
    }

}